package com.example.aircraft.panel;




import static java.lang.Thread.sleep;

import android.app.Activity;
import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;

import android.util.Log;
import android.view.View;
import android.view.Window;

import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;


import com.example.aircraft.GamePanel;
import com.example.aircraft.MyApplication;
import com.example.aircraft.R;
import com.example.aircraft.database.MySqlManager;
import com.example.aircraft.database.MySqliteHelper;
import com.example.aircraft.database.SQLiteInterface;
import com.example.aircraft.database.User_Data;
import com.example.aircraft.panel.EnrollPanel;
import com.example.aircraft.panel.MenuActivity;
import com.example.aircraft.panel.MenuActivity2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
//联机登录界面
public class DualSignPanel extends Activity implements View.OnClickListener{

    private static String userName;
    private static String passWord;
    private static User_Data user_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

//        new Thread(new NetConnP2()).start();
        setContentView(R.layout.activity_sign_panel2);
        ImageButton p1Signbtn = findViewById(R.id.P1signButton);
        p1Signbtn.setOnClickListener(this);
        ImageButton enrollBtn = findViewById(R.id.enrollBtn);
        enrollBtn.setOnClickListener(this);
        EditText editText1 = findViewById(R.id.editTextAccount);
        EditText editText2 = findViewById(R.id.editTextPassword);
        SpannableString ss1 = new SpannableString("请输入账号");
        SpannableString ss2 = new SpannableString("请输入密码");
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(15,true);
        ss1.setSpan(ass,0,ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText1.setHint(new SpannableString(ss1));
        ss2.setSpan(ass,0,ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        editText2.setHint(new SpannableString(ss2));
    }
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.P1signButton:

                boolean success = false;
                EditText accountText= findViewById(R.id.editTextAccount);
                userName = accountText.getText().toString();
                EditText passwordText= findViewById(R.id.editTextPassword);
                passWord = passwordText.getText().toString();
                new Thread(){
                    @Override
                    public void run(){
                        Log.i("client", "send message to server");
                        MyApplication.getWriter().println("333_789#"+userName+","+passWord);
                        MyApplication.getWriter().flush();
                        String content = "";
                        while(true){
                            try {
                                if (!((content=MyApplication.getIn().readLine())!=null)) break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            String[] command = content.split("[#,]");
                            if("111".equals(command[0])){
                                Log.d("REGTEST","登录成功");
                                    user_data = new User_Data(command[1],Integer.parseInt(command[2]),Integer.parseInt(command[3]),Integer.parseInt(command[4]),Integer.parseInt(command[2]));
                                    Intent intent = new Intent(DualSignPanel.this, GamePanel.class);
                                    startActivity(intent);
                                    DualSignPanel.this.finish();
                                    break;

                            }else if("404#1,1".equals(content)){
                                Log.d("REGTEST","登录失败");
                            }
                        }
                    }
                }.start();
                break;
            case R.id.enrollBtn:
                Intent intent = new Intent(DualSignPanel.this, EnrollPanel.class);
                startActivity(intent);
                this.finish();

                break;
            default:
                break;
        }
    }

    public static String getUserName(){
        return userName;
    }

    public static User_Data getUser_data() {
        return user_data;
    }
}